#!/bin/bash

DATE=$(date +%Y%m%d_%H%M)
DIR=portslackware-$DATE
SOURCE=code

rm -rf $DIR
rm -rf portslackware-*_*
rm -rf portslackware_database-*_*.tar.bz2

cd $SOURCE/portslackware-database
tar -cjvf ../../portslackware_database-$(date +%Y%m%d_%H%M).tar.bz2 *
cd ../..

mkdir -pv $DIR
mkdir -pv $DIR/usr/bin
mkdir -pv $DIR/usr/share/portslackware
mkdir -pv $DIR/usr/man/man1
mkdir -pv $DIR/usr/man/pt_BR/man1
mkdir -pv $DIR/install

cp $SOURCE/portslackware.sh $DIR/usr/bin/portslackware
cp $SOURCE/portscript.sh $DIR/usr/share/portslackware/
cp $SOURCE/slack-desc $DIR/install
cp portslackware_database-*_*.tar.bz2 $DIR/usr/share/portslackware/


gzip -c $SOURCE/manual > $DIR/usr/man/man1/portslackware.1.gz
gzip -c $SOURCE/manual-pt_br > $DIR/usr/man/pt_BR/man1/portslackware.1.gz

cd $DIR
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

chmod +x $DIR/usr/bin/portslackware

/sbin/makepkg -l y -c n ../$DIR-noarch-1.txz 

cd ..
rm -rf portslackware_database-*_*.tar.bz2
rm -rf ./$DIR